package com.jms.fu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jms.fu.exception.DAOException;
import com.jms.fu.model.bean.App;
import com.jms.fu.model.bo.AppBO;
import com.jms.fu.model.bo.AppDownBO;

public class GetHotAppAction extends Action{

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		try {
			PrintWriter out =  response.getWriter();
			String username = (String)request.getSession().getAttribute("username");
			String role = (String)request.getSession().getAttribute("role");
			AppBO appbo = new AppBO();
			ArrayList<App> listApp = new ArrayList<App>();
			listApp = appbo.getHotApp();
			
			String applistHTML = "";
			int num = listApp.size();
			AppDownBO appdown = new AppDownBO();
			for (int i = 0;i < num/5+1;i++ ) {
				String a="";
				if(i==0){
					a = " active";
				}
				
				applistHTML+="<div class='item"+a+"'><div class='row'>";
				for(int j=0;j<((!(num%4==0)&&i==num/5)?(num%4):4);j++){
					
					App temp = listApp.get(i*4+j);
					boolean check;
					if(username!=null){
					check = ((username.equals(temp.getDevId())) || (role
							.equals("Administrator")));
					}else{
						check = false;
					}
					ArrayList<String> avatar = appbo.getNumberOfFile(getServlet().getServletContext().getRealPath("/")+temp.getLink()+"/Avatar");
					String avatarLink;
					if(avatar.size()==0){
						avatarLink = "references/images/avatar.jpg"; 
					}else{
						avatarLink = temp.getLink()+"/Avatar/"+avatar.get(0);
					}
					
					boolean checkGetLink = false;
					String linkDownload = "";
					if(role!=null||username!=null){
						if (temp.getPrice() == 0 || appdown.checkAppOwnByUsername(username, temp.getAppId())
								|| temp.getDevId().equals(username)
								|| role.equals("Administrator")) {
							String appname = appbo.getNumberOfFile(
									getServlet().getServletContext().getRealPath("/")
											+ temp.getLink()).get(0);
							linkDownload = temp.getLink() + "/" + appname;
							checkGetLink=true;
						}
					}
					
					applistHTML +="<div class='col-sm-3'><div class='image_grid portfolio_8col'>"
							+ "<ul id='list' class='portfolio_list da-thumbs'><li>"
							+ "<img src='"+avatarLink+"'>"
									+ "<article class='da-animate da-slideFromTop' "
									+ "style='display: block;'> "
									+ "<h3 class='price'>"+(temp.getPrice() > 0 ? (temp.getPrice()+"$") : "Free")+"</h3> "
									+ (!checkGetLink?"<span class='link_buy'><a id='btn-buy-"+temp.getAppId()+"'  onclick='buyItem("+temp.getAppId()+")' title='Buy'></a></span>":
										"<span class='link_buy'><a id='btn-buy-"+temp.getAppId()+"' href='"+linkDownload+"' title='Download'></a></span>")
													+ "<span class='link_post'><a  onclick='addToCart("+temp.getAppId()+")' title='Add To Cart'></a></span>"
													+ "<span class='link_see'><a href='getAppDetail.jms?appid="+temp.getAppId()+"' title='See more'></a></span>"
													+ (check?"<span class='link_share'><a onclick='deleteAppAction("+temp.getAppId()+")' title='delete'></a></span>"
													+ "<span class='link_rate'><a onclick='getUpdateForm("+temp.getAppId()+")' title='update'></a></span>":"")
													+ "</article>"
													+ "<div><a class='item-g'>"+temp.getAppName()+"</a></div>"
															+ "</li></ul></div></div>";	
					
				}
				applistHTML+="</div></div>";
				
			}
			out.print(applistHTML);
			out.close();
		} catch (IOException | DAOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
}
